package com.github.baichuan.application.scaffold.domain.app;

import com.github.baichuan.application.scaffold.domain.application.Application;
import com.github.baichuan.application.scaffold.domain.application.ApplicationService;
import com.github.baichuan.application.scaffold.dto.app.AppVersionForm;
import com.github.baichuan.application.scaffold.dto.app.CurrentVersion;
import com.github.baichuan.application.scaffold.dto.app.UpgradeVersion;
import com.github.baichuan.application.scaffold.jpa.app.AppRepository;
import com.github.baichuan.application.scaffold.jpa.app.AppVersionRepository;
import com.github.baichuan.mapping.BeanMapper;
import com.github.baichuan.web.dto.Result;
import com.github.baichuan.web.error.NoSuchEntityException;
import org.checkerframework.checker.units.qual.A;
import org.semver4j.Semver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
@Transactional(rollbackFor = Exception.class)
public class AppVersionService {
    @Autowired
    private AppVersionRepository versionRepository;
    @Autowired
    private AppRepository appRepository;
    @Autowired
    private AppService appService;
    @Autowired
    private ApplicationService applicationService;

    public Page<AppVersion> query(Specification<AppVersion> specification, Pageable pageable){
        return versionRepository.findAll(specification, pageable);
    }

    public AppVersion find(int id){
        return versionRepository.findById(id).orElseThrow(() -> new NoSuchEntityException("APP版本不存在"));
    }

    public Result add(String appId, AppVersionForm form){
        App app = appService.find(appId);

        AppVersion currentVersion = app.getLatestVersion();
        if(currentVersion != null) {
            Semver currentSemver = new Semver(currentVersion.getVersion());
            if(currentSemver.isGreaterThanOrEqualTo(form.getVersion().trim())) {
                return Result.fail("发布版本号必须大于当前版本");
            }
        }

        AppVersion version = new AppVersion();
        version.setApp(app);
        version.setVersion(form.getVersion());
        version.setIntro(form.getIntro().trim());
        version.setUpgradeMode(form.getUpgradeMode());
        version.setReleaseTime(LocalDateTime.now());
        version.setFile(form.getFile());
        versionRepository.save(version);

        app.setLatestVersion(version);
        appRepository.save(app);

        return Result.success("版本发布成功", version.getId());
    }

    public Result remove(int id){
        AppVersion version = find(id);
        App app = version.getApp();

        if(app.getLatestVersion().getVersion().equals(version.getVersion())) {
            return Result.fail("最新版本不能删除");
        }

        versionRepository.delete(version);
        return Result.success("版本删除成功");
    }

    /**
     * 版本更新检测
     * @return
     */
    public Optional<UpgradeVersion> upgrade(String applicationCode, CurrentVersion version){
        Application application = applicationService.findByCode(applicationCode);
        App app = appService.find(application, version.getPlatform(), version.getCode());
        Optional<AppVersion> optional = versionRepository.findLatestVersion(app);
        if(optional.isPresent()){
            AppVersion latestVersion = optional.get();
            Semver latestSemVer = new Semver(latestVersion.getVersion());
            Semver currentSenVer = new Semver(version.getVersion());
            if(latestSemVer.isGreaterThan(currentSenVer)){
                UpgradeVersion upgradeVersion = BeanMapper.map(latestVersion, UpgradeVersion.class);
                return Optional.of(upgradeVersion);
            }
        }
        return Optional.empty();
    }
}
